﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// http://codekata.com/kata/kata02-karate-chop/
/// </summary>
namespace RandomKatas.Kata02
{
    public interface IBinaryChop
    {
        int Chop(int searchingFor, int[] inList);
    }

    public class GetToDaBinary
    {
        public static IBinaryChop Choppa(string name)
        {
            switch (name.ToLowerInvariant())
            {
                case "strategy1":
                    return new Strategy1();

                case "strategy2":
                    return new Strategy2();

                case "strategy3":
                    return new Strategy3();

                case "strategy4":
                    return new Strategy4();

                case "strategy5":
                    return new Strategy5();

                default:
                    throw new ArgumentException();
            }
        }
    }

    public class Strategy1 : IBinaryChop
    {
        public int Chop(int searchingFor, int[] inList)
        {
            if (inList == null || inList.Length == 0) return -1;

            var low = 0;
            var high = inList.Length;
            while (low < high)
            {
                var mid = (high - low) / 2 + low;
                if (inList[mid] == searchingFor) return mid;
                if (inList[mid] < searchingFor)
                    low = mid + 1;
                else
                    high = mid;
            }

            return -1;
        }
    }

    public class Strategy2 : IBinaryChop
    {
        public int Chop(int searchingFor, int[] inList)
        {
            throw new NotImplementedException();
        }
    }

    public class Strategy3 : IBinaryChop
    {
        public int Chop(int searchingFor, int[] inList)
        {
            throw new NotImplementedException();
        }
    }

    public class Strategy4 : IBinaryChop
    {
        public int Chop(int searchingFor, int[] inList)
        {
            throw new NotImplementedException();
        }
    }

    public class Strategy5 : IBinaryChop
    {
        public int Chop(int searchingFor, int[] inList)
        {
            throw new NotImplementedException();
        }
    }
}
